package org.firebug.spring.boot.validator.controller;

import lombok.Data;
import lombok.experimental.Accessors;
import org.firebug.common.core.api.Response;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Response<User> query(@PathVariable("id") Integer id) {
        return Response.ok(new User().setId(id).setName("felix"));
    }

    @RequestMapping(value = {"/users", "/list"}, method = RequestMethod.GET)
    public Response<List<User>> list() {
        List<User> users = Arrays.asList(new User().setId(1).setName("felix"));
        return Response.ok(users);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Response remove(@PathVariable("id") Integer id) {
        return Response.ok();
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public Response<User> save(@Validated(User.Save.class) @RequestBody User user) {
        return Response.ok();
    }
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public Response<User> save(@PathVariable("id") Integer id, @Validated(User.Save.class) @RequestBody User user) {
        return Response.ok();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Response<User> modify(@PathVariable("id") Integer id, @Validated(User.Modify.class) @RequestBody User user) {
        return Response.ok();
    }

    @Data
    @Accessors(chain = true)
    static class User {
        @NotBlank
        private Integer id;
        @NotBlank(message = "not blank", groups = {Save.class})
        private String name;

        interface Save {}
        interface Modify {}
    }
}
